<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['purchase:pomMstr:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['purchase:pomMstr:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['purchase:pomMstr:remove']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="single"
          @click="handleSubmit"
          v-hasPermi="['purchase:pomMstr:upload']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['purchase:pomMstr:export']"
        >
          导出
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid green;
              background-color: green;
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">未审核</span>
        </div>
      </el-col>
      <el-col :span="1.5">
        <div
          style="
            border: 1px solid white;
            width: 80px;
            height: 28px;
            line-height: 28px;
            display: flex;
          "
        >
          <div
            style="
              margin-top: 6px;
              width: 15px;
              height: 15px;
              border: 1px solid #bb0331;
              background-color: #bb0331;
              border-radius: 50%;
              margin-right: 10px;
            "
          ></div>
          <span style="display: block; font-size: 14px">已审核</span>
        </div>
      </el-col>
      <el-col :span="2.2" style="position: absolute; right: 35px; top: 0">
        <Page
          style="margin-right: 0px"
          :current="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          :total="total"
          @on-change="pageChange"
          simple
        ></Page>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="450px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="98px"
      >
        <el-col :span="24">
          <el-form-item label="建立日期">
            <el-date-picker
              v-model="daterangePomJlrq"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              :clearable="false"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="采购单号">
            <el-input
              v-model="queryParams.pomDjbh"
              placeholder="请输入采购单号"
              clearable
              style="width: 240px"
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入品名规格或客户编码"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="单据状态">
            <el-radio-group v-model="queryParams.pomZtbz">
              <el-radio
                v-for="dict in ZtbzOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
                >{{ dict.dictLabel }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <!-- 头表Table -->
    <el-table
      highlight-current-row
      v-loading="loading"
      :data="pomMstrList"
      :height="tableH"
      :cell-style="iCellStyle"
      border
      stripe
      @row-click="clickPodDet"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="采购单号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="pomDjbh"
      />
      <el-table-column
        label="入库类型"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomType"
      >
        <template #default="scope">
          <dict-tag :options="psTypeOptions" :value="scope.row.pomType" />
        </template>
      </el-table-column>
      <el-table-column
        label="入库仓库"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomCkdm"
      >
        <template #default="scope">
          <dict-tag :options="storageOptions" :value="scope.row.pomCkdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomDesc"
      />
      <el-table-column
        label="撤回原因"
        :show-overflow-tooltip="true"
        width="180"
        align="center"
        prop="pomBackdesc"
      />
      <el-table-column
        label="单据状态"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomZtbz"
      >
        <template #default="scope">
          <dict-tag :options="ZtbzOptionss" :value="scope.row.pomZtbz" />
        </template>
      </el-table-column>
      <el-table-column
        label="交货日期"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="pomJhrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pomJhrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="pomJlrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pomJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="审核人员"
        :show-overflow-tooltip="true"
        align="center"
        prop="pomShry"
      />
      <el-table-column
        label="审核日期"
        :show-overflow-tooltip="true"
        width="150"
        align="center"
        prop="pomShrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.pomShrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="80"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAddChildren(scope.row)"
            :disabled="scope.row.pomZtbz != '0'"
            v-hasPermi="['purchase:pomMstr:edit']"
          >
            添加物料
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <portal-target :name="portalId"></portal-target>
    <portal :to="openChildren ? 'dialog-' + portalId : portalId">
      <!-- 详细表Table -->
      <el-tabs v-model="activeTab">
        <el-tab-pane label="采购明细" name="first">
          <el-table
            v-loading="loading"
            :data="podDetList"
            border
            stripe
            :height="tableHChild"
            :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
            @selection-change="handleSelectionChange"
          >
            <el-table-column label="#" width="40" align="center" />
            <el-table-column
              label="请购单号"
              width="150"
              align="center"
              prop="podQgdh"
            />
            <el-table-column
              label="销售单号"
              width="150"
              align="center"
              prop="podSodh"
            />
            <el-table-column
              label="物料代码"
              width="150"
              align="center"
              prop="podWldm"
            />
            <el-table-column label="品名规格" min-width="400" prop="itmPmgg" />
            <el-table-column
              label="采购属性"
              width="100"
              align="center"
              prop="podCgsx"
            >
              <template #default="scope">
                <dict-tag :options="CgsxOptions" :value="scope.row.podCgsx" />
              </template>
            </el-table-column>
            <el-table-column
              v-if="curRow.pomType == 'RK05'"
              label="外发厂商"
              align="center"
              prop="podCsm"
              width="120"
            >
              <template #default="scope">
                {{
                  scope.row.podCsm
                    ? csOptions.find((o) => scope.row.podCsm == o.dictValue)
                        .csmCsmc
                    : ""
                }}
              </template>
            </el-table-column>
            <el-table-column
              label="最小包装"
              width="120"
              align="center"
              prop="itmMinbz"
            />
            <el-table-column
              label="采购数量"
              width="120"
              align="center"
              prop="podCgsl"
            />
            <el-table-column
              label="单位"
              :show-overflow-tooltip="true"
              width="100"
              align="center"
              prop="itmDw"
            >
              <template #default="scope">
                <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
              </template>
            </el-table-column>
            <el-table-column
              label="建立人员"
              width="80"
              align="center"
              prop="podJlry"
            />
            <el-table-column
              label="建立日期"
              width="180"
              :show-overflow-tooltip="true"
              align="center"
              prop="podJlrq"
            >
              <template #default="scope">
                <span>{{ parseTime(scope.row.podJlrq, "{y}-{m}-{d}") }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="操作"
              fixed="right"
              align="center"
              width="80"
              class-name="small-padding fixed-width"
            >
              <template #default="scope">
                <el-button
                  size="mini"
                  type="text"
                  :disabled="childDisabled"
                  @click="handPodDelete(scope.row)"
                  v-hasPermi="['purchase:podDet:remove']"
                  icon="el-icon-delete"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="采购统计" name="second">
          <el-table
            v-loading="loading"
            :height="tableHChild"
            :data="podDetCountList"
            :header-cell-style="{ 'text-align': 'center', padding: '2px' }"
            border
            stripe
          >
            <el-table-column label="#" width="40" align="center" />
            <el-table-column label="物料代码" align="center" prop="podWldm" />
            <el-table-column label="品名规格" min-width="400" prop="itmPmgg" />
            <el-table-column label="采购属性" align="center" prop="podCgsx">
              <template #default="scope">
                <dict-tag :options="CgsxOptions" :value="scope.row.podCgsx" />
              </template>
            </el-table-column>
            <el-table-column label="最小包装" align="center" prop="itmMinbz" />
            <el-table-column label="采购数量" align="center" prop="podCgsl" />
            <el-table-column
              label="单位"
              :show-overflow-tooltip="true"
              align="center"
              prop="itmDw"
            >
              <template #default="scope">
                <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </portal>
    <!-- 添加物料dialog-->
    <el-dialog
      v-model="openChildren"
      title="   "
      customClass="customWidth"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 添加物料搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-form-item label="请购单号" prop="podQgdh">
          <el-input
            v-model="queryChildParams.podQgdh"
            placeholder="请输入请购单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="物料代码" prop="podWldm">
          <el-input
            v-model="queryChildParams.podWldm"
            placeholder="请输入物料代码"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户编码" prop="somCustwldm">
          <el-input
            v-model="queryChildParams.somCustwldm"
            placeholder="请输入请购单号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="综合查询" prop="itmIntegrated">
          <el-input
            v-model="queryChildParams.itmIntegrated"
            placeholder="请输入综合查询"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item style="justify-content: flex-end">
          <el-button type="primary" @click="serachChildren">查询</el-button>
        </el-form-item>
      </el-form>
      <!--待接收物料-->
      <el-form>
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="serachlistPodDet"
          @selection-change="handleSelectionChangeC"
          border
          stripe
          @row-click="clickSelection"
          ref="checkTable"
          :height="tableH"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            fixed="left"
            type="selection"
            width="55"
            align="center"
          />
          <el-table-column
            fixed="left"
            label="#"
            type="index"
            width="40"
            align="center"
          />
          <el-table-column
            fixed="left"
            label="物料代码"
            :show-overflow-tooltip="true"
            align="center"
            prop="podWldm"
          />
          <el-table-column
            fixed="left"
            label="品名规格"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            fixed="left"
            label="采购数量"
            :show-overflow-tooltip="true"
            align="center"
            prop="qgdQgsl"
          />
          <el-table-column
            fixed="left"
            label="同物料汇总数"
            :show-overflow-tooltip="true"
            align="center"
            prop="totalQgsl"
          />
          <el-table-column
            v-if="curRow.pomType == 'RK05'"
            label="外发厂商"
            align="center"
            prop="podCsm"
          >
            <template #default="scope">
              <el-select
                size="mini"
                @focus="getItmCs(scope.row)"
                v-model="scope.row.podCsm"
                clearable
                filterable
                placeholder="请选择厂商代码"
              >
                <el-option
                  v-for="item in itmCsOptions"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            label="申请数量"
            :show-overflow-tooltip="true"
            align="center"
          >
            <template #default="scope">
              <el-input
                type="number"
                class="inputName"
                v-model="scope.row.podCgsl"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column
            fixed="left"
            label="采购属性"
            :show-overflow-tooltip="true"
            align="center"
            prop="podCgsx"
          >
            <template #default="scope">
              <dict-tag :options="CgsxOptions" :value="scope.row.podCgsx" />
            </template>
          </el-table-column>
          <el-table-column
            fixed="left"
            label="单位"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            fixed="left"
            label="最小包装"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmMinbz"
          />
          <el-table-column
            label="销售单号"
            :show-overflow-tooltip="true"
            align="center"
            prop="podSodh"
          />
          <el-table-column
            label="客户编码"
            :show-overflow-tooltip="true"
            align="center"
            prop="somCustwldm"
          />
          <el-table-column
            label="客户"
            :show-overflow-tooltip="true"
            align="center"
            prop="somKhdm"
          >
            <template #default="scope">
              <dict-tag :options="customerOptions" :value="scope.row.somKhdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="需求日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="qgmXqrq"
          />
          <el-table-column
            label="申请人员"
            :show-overflow-tooltip="true"
            align="center"
            prop="qgmJlry"
          />
          <el-table-column
            label="申请日期"
            :show-overflow-tooltip="true"
            align="center"
            prop="qgmJlrq"
          />
          <el-table-column
            label="请购单号"
            :show-overflow-tooltip="true"
            align="center"
            prop="podQgdh"
          />
        </el-table>
        <!--采购明细-->
        <portal-target :name="'dialog-' + portalId"></portal-target>
        <!-- 底部确定取消 -->
        <el-form-item
          style="margin-top: 25px; display: flex; justify-content: flex-end"
        >
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="handAddPod">确认</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 添加或修改生产采购对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="采购单号" prop="pomDjbh">
          <el-input v-model="form.pomDjbh" disabled />
        </el-form-item>
        <el-form-item label="需求日期" prop="pomJhrq">
          <el-date-picker
            clearable
            size="small"
            v-model="form.pomJhrq"
            type="date"
            :clearable="false"
            value-format="yyyy-MM-dd"
            placeholder="选择需求日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="入库类型" prop="pomType">
          <el-select v-model="form.pomType" placeholder="请选择采购类型">
            <el-option
              v-for="dict in psTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="入库仓库" prop="pomCkdm">
          <el-select v-model="form.pomCkdm" placeholder="请选择入库仓库">
            <el-option
              v-for="dict in storageOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单据备注" prop="pomDesc">
          <el-input type="textarea" v-model="form.pomDesc" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPomMstr,
  getPomMstr,
  delPomMstr,
  addPomMstr,
  updatePomMstr,
  exportPomMstr,
  changePomMstr,
} from "@/api/purchase/pomMstr";
import { getPstorageType } from "@/api/repertory/putstorage";
import { getstorageType } from "@/api/repertory/Storage";
import { getAllCgList } from "@/api/project/mstr";
import {
  listsPodDet,
  serachlistPod,
  addPodDet,
  delPodDet,
} from "@/api/purchase/podDet";
import { getAllCustomerTwo } from "@/api/project/customer";
import { getItmRelevanceCs } from "@/api/project/materiel";
import mixin from "@/mixins/common";

export default {
  name: "PomMstr",
  mixins: [mixin.portal, mixin.csOptions],
  data() {
    return {
      tableH:
        document.body.clientHeight > 900
          ? document.body.clientHeight / 2 - 96
          : document.body.clientHeight / 2 - 95,
      tableHChild: document.body.clientHeight / 2 - 150,
      // 遮罩层
      loading: true,
      //默认子表
      activeTab: "first",
      // 详细表List
      podDetList: [],
      podDetCountList: [],
      //采购单号
      podDjbhc: null,
      pomCkdmc: null,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      pomDjbhs: [],
      clickPomMstr: [],
      itmCsOptions: [],
      // 非单个禁用
      single: true,
      childDisabled: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      //搜索子项条数
      totalChild: 0,
      // 生产采购表格数据
      pomMstrList: [],
      //搜索子项List
      serachlistPodDet: [],
      //选中的数据
      selectionPodDet: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否显示添加弹出层
      openChildren: false,
      // 建立日期时间范围
      daterangePomJlrq: [],
      // 入库类型字典
      psTypeOptions: [],
      // 入库仓库字典
      storageOptions: [],
      //采购属性字典
      CgsxOptions: [],
      // 单位字典
      unitFlagOptions: [],
      // 状态标志字典
      ZtbzOptions: [],
      // 显示单据状态标志字典
      ZtbzOptionss: [],
      //客户字典
      customerOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 15 : 25,
        pageType: 1,
        pomDjbh: null,
        pomZtbz: "1",
        pomJlrq: null,
        pomType: null,
      },
      //添加物料查询参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 25,
        podQgdh: null,
        podWldm: null,
        pomCkdm: null,
        pomType: null,
        somCustwldm: null,
        itmIntegrated: null,
        qgdCgsx: null,
      },
      // 表单参数
      form: {},
      curRow: {},
      // 表单校验
      rules: {
        pomType: [
          { required: true, message: "入库类型不能为空", trigger: "change" },
        ],
        pomCkdm: [
          { required: true, message: "指令代码不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    //入库类型字典
    getPstorageType(0).then((response) => {
      this.psTypeOptions = response.data;
      for (let i of this.psTypeOptions) {
        i.listClass = "default";
      }
    });
    this.getDicts("sys_purchase_state").then((response) => {
      for (let i of response.data) {
        this.ZtbzOptionss.push(i);
      }
      this.ZtbzOptions = response.data;
      const flag = { dictLabel: "新增", dictValue: "0", listClass: "default" };
      this.ZtbzOptionss.push(flag);
    });
    //入库仓库字典
    getstorageType(0).then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.storageName;
        i.dictValue = i.storageId + "";
        i.listClass = "default";
      }
      this.storageOptions = response.data;
    });
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //采购属性
    getAllCgList().then((response) => {
      this.CgsxOptions = response.data;
      for (let i of this.CgsxOptions) {
        i.dictLabel = i.cgName;
        i.dictValue = i.cgId + "";
        i.listClass = "default";
      }
    });
    //客户字典
    getAllCustomerTwo().then((response) => {
      for (let i of response.data) {
        i.dictLabel = i.cusName;
        i.dictValue = i.cusCode + "";
        i.listClass = "default";
      }
      this.customerOptions = response.data;
    });
  },
  methods: {
    /** 分页跳转 */
    pageChange(page) {
      this.queryParams.pageNum = page;
      this.getList();
    },
    /** 查询生产采购列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangePomJlrq && "" != this.daterangePomJlrq) {
        this.queryParams.params["beginPomJlrq"] = this.daterangePomJlrq[0];
        this.queryParams.params["endPomJlrq"] = this.daterangePomJlrq[1];
      }
      listPomMstr(this.queryParams).then((response) => {
        this.podDetList = [];
        this.podDetCountList = [];
        this.pomMstrList = response.rows;
        this.total = response.total;
        this.loading = false;
        if (row != null) {
          this.clickPodDet(row);
        } else if (this.pomMstrList != null && this.pomMstrList.length > 0) {
          this.clickPodDet(this.pomMstrList[0]);
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.openChildren = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pomId: null,
        pomDjbh: null,
        pomJhrq: new Date(),
        pomZtbz: null,
        pomDesc: null,
        pomBackdesc: null,
        pomJlry: null,
        pomJlrq: null,
        pomShry: null,
        pomShrq: null,
        pomTjflag: null,
        pomCkdm: null,
        pomType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePomJlrq = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    iCellStyle: function ({ row, column, rowIndex, columnIndex }) {
      //未审核
      if (row.pomZtbz === 1) {
        return "padding:0px 0;color:green";
      }
      //已审核
      else if (row.pomZtbz === 2) {
        return "padding:0px 0;color:#bb0331";
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.pomId);
      this.pomDjbhs = selection.map((item) => item.pomDjbh);
      this.clickPomMstr = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    getItmCs(row) {
      getItmRelevanceCs(row.podWldm).then((response) => {
        this.itmCsOptions = response.data;
        for (let item of this.itmCsOptions) {
          item.dictLabel = item.csmCsmc;
          item.listClass = "default";
          item.dictValue = item.csmCsdm;
        }
      });
    },
    clickSelection(row) {
      this.$refs.checkTable.toggleRowSelection(row);
    },
    //多选框选中子项数据
    handleSelectionChangeC(selection) {
      this.selectionPodDet = selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产采购";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      for (let i of this.clickPomMstr) {
        if (i.pomZtbz != 0 || i.pomTjflag != 0) {
          this.msgError("单据不是新增状态,无法修改");
          return;
        }
      }
      this.reset();
      const pomId = row.pomId || this.ids;
      getPomMstr(pomId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产采购";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询生产采购";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.pomId != null) {
            updatePomMstr(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPomMstr(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      for (let i of this.clickPomMstr) {
        if (i.pomZtbz != 0 || i.pomTjflag != 0) {
          this.msgError("单据不是新增状态,无法删除");
          return;
        }
      }
      const pomIds = row.pomId || this.ids;
      const podDjbh = this.pomDjbhs;
      this.$confirm(
        '是否确认删除采购单号: <"' + podDjbh + '"> 及子项数据?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delPomMstr(pomIds, podDjbh);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 提交操作按钮*/
    handleSubmit() {
      const data = { podDjbh: this.clickPomMstr[0].pomDjbh };
      listsPodDet(data).then((res) => {
        if (res.data.detail.length > 0) {
          if (this.clickPomMstr[0].pomZtbz != 0) {
            this.msgError(
              "采购订单: " +
                this.clickPomMstr[0].pomDjbh +
                " 不是新增状态,无法提交~"
            );
            return;
          }
          const podDjbh = this.pomDjbhs;
          changePomMstr(podDjbh, 0, null).then((response) => {
            this.msgSuccess("提交成功~");
            this.getList();
          });
        } else {
          this.msgError(
            "采购订单: " +
              this.clickPomMstr[0].pomDjbh +
              " 未添加采购明细,无法提交~"
          );
        }
      });
    },
    /** 查看详细表数据 */
    clickPodDet(row) {
      if (row.pomZtbz !== null && row.pomZtbz !== undefined)
        this.childDisabled = row.pomZtbz != 0;
      const data = { podDjbh: row.pomDjbh };
      listsPodDet(data).then((response) => {
        this.podDetList = response.data.detail;
        this.podDetCountList = response.data.total;
      });
    },
    /** 添加子项物料 */
    handleAddChildren(row) {
      this.curRow = row;
      this.podDjbhc = row.pomDjbh;
      this.pomType = row.pomType;
      this.queryChildParams.pomCkdm = row.pomCkdm;
      this.queryChildParams.pomType = row.pomType;
      this.serachlistPodDet = [];
      this.openChildren = true;
    },
    //搜索待接收的物料
    serachChildren() {
      this.queryChildParams.pageNum = 1;
      serachlistPod(this.queryChildParams).then((response) => {
        this.serachlistPodDet = response.rows;
        this.totalChild = response.total;
      });
    },
    //添加Pod按钮操作
    handAddPod() {
      if (this.selectionPodDet.length > 0) {
        for (let i of this.selectionPodDet) {
          if (i.podCgsl == null || i.podCgsl <= 0) {
            this.msgError(
              "物料代码: <" + i.podWldm + "> 数量输入错误,请核对！"
            );
            return;
          }
          if (this.curRow.pomType == "RK05" && !i.podCsm) {
            this.msgError(
              "物料代码: <" + i.podWldm + "> 外发厂商不能为空,请选择！"
            );
            return;
          }

          i.podDjbh = this.podDjbhc;
        }
        addPodDet(this.selectionPodDet).then((response) => {
          const row = { pomDjbh: this.podDjbhc };
          this.clickPodDet(row);
          this.serachChildren();
          this.msgSuccess("添加成功");
        });
      } else {
        this.msgError("请勾选需要申请的物料~");
      }
    },
    /** 删除子项物料*/
    handPodDelete(row) {
      const pomDjbh = { pomDjbh: row.podDjbh };
      listPomMstr(pomDjbh).then((res) => {
        if (res.rows[0].pomZtbz != "0") {
          this.msgError(
            "采购单号: [" +
              res.rows[0].pomDjbh +
              "]不是新增状态,无法删除该单据明细数据~"
          );
          return;
        } else {
          this.$confirm("是否确认删除此数据项?", "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(function () {
              return delPodDet(row.podId, row.podQgid);
            })
            .then(() => {
              const row = { pomDjbh: this.podDjbhc };
              this.clickPodDet(row);
              this.msgSuccess("删除成功");
            })
            .catch(() => {});
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有生产采购数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportPomMstr(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
